//
//  SupplyAnalyzeDetailController.m
//  Manager
//
//  Created by Mr.Lee on 2018/6/4.
//  Copyright © 2018年 Reabam. All rights reserved.
//

#import "SupplyAnalyzeDetailController.h"
#import "LoginViewController.h"
#import "CashierHandOverModel.h"
#import "Global.h"
#import "SupplyAnalyzeDetailCell.h"
#import "TSFooterRefresh.h"                 // 刷新
#import "TSHeaderRefresh.h"
#import "CommercialModel.h"


@interface SupplyAnalyzeDetailController ()<UIGestureRecognizerDelegate, UINavigationControllerDelegate, UITextFieldDelegate, UISearchBarDelegate, UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, weak) UIView *opView;
@property (nonatomic, weak) UILabel *timeLabel;

@end

@implementation SupplyAnalyzeDetailController
{
    NSMutableArray*modelArray;
    BOOL saveAccount;
    int pageNumber;
    NSString *pageSize;
    NSString *result;
    NSString*TotalCount;
    UIView*noDataView;
    
    UITableView *table;
    
    UIView*_dateView;
    UIButton*_dateConfirmBtn;
    UIDatePicker*_datePicker;
    UITextField *startTextField;
    UITextField *endTextField;
    NSString *StartOrEnd;   
}



#pragma mark -
- (void)viewDidLoad {
    [super viewDidLoad];
    pageSize = @"20";
    modelArray =[NSMutableArray array];

    UILabel *timeLabel = [[UILabel alloc] init];
    timeLabel.frame = CGRectMake(0, 0, ScreenWidth, 30);
    self.timeLabel = timeLabel;
    timeLabel.font = [UIFont systemFontOfSize:13.0];
    timeLabel.textColor = lanse;// [UIColor colorWithHexString:@"cccccc"];
    timeLabel.textAlignment = NSTextAlignmentCenter;
    timeLabel.backgroundColor = [UIColor colorWithHexString:@"f4f4f4"];
    if ([self.beginDate isEqualToString:self.endDate]) {
        timeLabel.text = self.beginDate;
    } else{
        timeLabel.text = [NSString stringWithFormat:@"%@~%@", self.beginDate, self.endDate];
    }
    [self.view addSubview:timeLabel];
    
    [self addTable];
    
    // 集成刷新控件
    [self setUpRefresh];
    
    // 数据请求
    [self requestData];
}
#pragma mark -
#pragma mark 时间处理

- (NSString *)formartWithDateString:(NSString *)date{
    NSString *newDate= [date substringToIndex:10];
    return newDate;
}
// 获取今日
- (NSString *)getCurrentTime {
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setTimeZone:timeZone];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateTime = [formatter stringFromDate:[NSDate date]];
    return dateTime;
}

#pragma mark -
#pragma mark talbleView
-(void)addTable{
    
    [table removeFromSuperview];
    table = [[UITableView alloc]initWithFrame:CGRectMake(0, 30, ScreenWidth, ScreenHeight-64-30) style:UITableViewStylePlain];
    table.separatorStyle=UITableViewCellSeparatorStyleNone;
    table.delegate=self;
    table.dataSource=self;
    [self.view addSubview: table];
    
}


// 刷新
- (void)setUpRefresh {
    table.mj_footer = [TSFooterRefresh footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreProductList)];
}

-(void)requestData{
    pageNumber=1;
    [noDataView removeFromSuperview];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setValue:Token forKey:@"tokenId"];
    [param setValue:@"1" forKey:@"pageIndex"];
    [param setValue:pageSize forKey:@"pageSize"];
    //    排序字段 orderCount按单数 amount按金额
//    [param setValue:orderField forKey:@"orderField"];
//    //    排序方式 asc升序 desc降序
//    [param setValue:_orderSort forKey:@"orderSort"];
    
    [param setValue:_beginDate forKey:@"beginDate"];
    [param setValue:_endDate forKey:@"endDate"];
    
    NSString *url = nil;
    // 品类
    if ([self.type isEqualToString:@"order"]) {
        url = @"report/item/itemTypeSalesDetail";
        [param setValue:_mTypeCode forKey:@"mTypeCode"];
        // 商品销量
    } else{
        [param setValue:_itemId forKey:@"itemId"];
        [param setValue:_specId forKey:@"specId"];
        url = @"report/item/itemSalesDetail";
    }
    
    NSLog(@"%@", param);
    [MBProgressHUD showIndeterminateHudSub:@"" delay:30 view:self.view];
    [HttpRequest requestData:url params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
        NSLog(@"====%@",severDataDict.dataDict);
        if (isSuccess) {
            [modelArray removeAllObjects];
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [MBProgressHUD hideProgressHUDInternal:self.view];
                NSMutableArray* arrr=[[NSMutableArray alloc] init];
                arrr=[severDataDict.dataDict objectForKey:@"DataLine"];
                if (arrr.count == 0) {
                    noDataView=[[UIView alloc]initWithFrame:CGRectMake(0, (ScreenHeight-64-49)/2-50, ScreenWidth, 80)];
                    [self.view addSubview:noDataView];
                    UIImageView*noImage=[[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth/2-47, 0, 94, 51)];
                    
                    UIImage *image1 = nil;
                    if ([self.type isEqualToString:@"shangpin"]) {
                        image1= [UIImage imageNamed:@"meiyoushuju"];
                    } else{
                        image1= [UIImage imageNamed:@"meiyoushuju"];
                    }
                    
                    noImage.image = image1;
                    [noDataView addSubview:noImage];
                    UILabel*noLabel=[[UILabel alloc]initWithFrame:CGRectMake(0,65, ScreenWidth, 14)];
                    noLabel.text=@"没有列表数据！";
                    noLabel.textColor=zhonghuiColor; noLabel.font=[UIFont systemFontOfSize:14];
                    noLabel.textAlignment=NSTextAlignmentCenter;
                    [noDataView addSubview:noLabel];
                } else{
                    for (NSDictionary* dicc in arrr ) {
                        CommercialModel *model =[CommercialModel  mj_objectWithKeyValues:dicc];
                        [modelArray addObject:model];
                    }
                }
                
                [table reloadData];
                
                NSString *orderCountTotal = [NSString stringWithFormat:@"%@",[severDataDict.dataDict objectForKey:@"orderCountTotal"]];
                NSString *salesTotal = [NSString stringWithFormat:@"%@",[severDataDict.dataDict objectForKey:@"salesTotal"]];
                NSString *beginDate = [NSString stringWithFormat:@"%@",[severDataDict.dataDict objectForKey:@"beginDate"]];
                NSString *endDate = [NSString stringWithFormat:@"%@",[severDataDict.dataDict objectForKey:@"endDate"]];
                
                TotalCount= [NSString stringWithFormat:@"%@", [severDataDict.dataDict objectForKey:@"TotalCount"]];
                if (TotalCount.intValue < pageSize.intValue) {
                    [table.mj_footer endRefreshingWithNoMoreData];
                } else if(TotalCount.intValue > pageSize.intValue){
                    [table.mj_footer resetNoMoreData];
                }
            }else{
                [MBProgressHUD hideProgressHUDInternal:self.view];
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
                    LoginViewController *loginVC=[[LoginViewController alloc] init];//2.0{9个参数 少2个}
                    [self.navigationController pushViewController:loginVC animated:YES];
                }
                NSString *subStr = [severDataDict.dataDict objectForKey:@"ResultString"];
                [MBProgressHUD showTextHudSub:subStr view:self.view];
            }
            
        } else {
            [MBProgressHUD hideProgressHUDInternal:self.view];
            NSString *subStr = [severDataDict.dataDict objectForKey:@"ResultString"];
            [MBProgressHUD showTextHudSub:subStr view:self.view];
        }
        
    }];
    
}

// 上拉刷新
- (void)loadMoreProductList{
    if (TotalCount.floatValue / pageSize.floatValue > pageNumber) {
        pageNumber++;
        
        NSUserDefaults *user1 = [NSUserDefaults standardUserDefaults];
        NSString* tokenId = [user1 objectForKey:@"tokenId"];
        NSMutableDictionary *params = [NSMutableDictionary new];
        [params setValue:tokenId forKey:@"tokenId"];
        [params setValue:[NSString stringWithFormat:@"%d" ,pageNumber] forKey:@"pageIndex"];
        [params setValue:pageSize forKey:@"pageSize"];
//        [params setValue:orderField forKey:@"orderField"];
        //    排序方式 asc升序 desc降序
//        [params setValue:_orderSort forKey:@"orderSort"];
        
        [params setValue:_beginDate forKey:@"beginDate"];
        [params setValue:_endDate forKey:@"endDate"];
        
        NSString *url = nil;
        // 品类
        if ([self.type isEqualToString:@"order"]) { // 订货单
            url = @"report/item/itemTypeSalesDetail";
            [params setValue:_mTypeCode forKey:@"mTypeCode"];
            // 商品销量
        } else{
            [params setValue:_itemId forKey:@"itemId"];
            [params setValue:_specId forKey:@"specId"];
            url = @"report/item/itemSalesDetail";
        }
        [MBProgressHUD showIndeterminateHudSub:@"" delay:30 view:self.view];
        [HttpRequest requestData:@"report/SalesSummary/statisticsList" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
            if (isSuccess) {
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                    [MBProgressHUD hideProgressHUDInternal:self.view];
                    NSMutableArray* arrr=[[NSMutableArray alloc]init];
                    arrr=[severDataDict.dataDict objectForKey:@"DataLine"];
                    for (NSDictionary* dicc in arrr ) {
                        CommercialModel *model =[CommercialModel  mj_objectWithKeyValues:dicc];
                        [modelArray addObject:model];
                    }
                    
                    
                    [table reloadData];
                    TotalCount=[severDataDict.dataDict objectForKey:@"TotalCount"];
                    if (TotalCount.intValue < pageSize.intValue) {
                        [table.mj_footer endRefreshingWithNoMoreData];
                    } else if(TotalCount.intValue >pageSize.intValue){
                        [table.mj_footer resetNoMoreData];
                    }
                }else {
                    NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                    [MBProgressHUD showTextHudSub:requestResultStr view:self.view];
                }
                
            } else {
                [MBProgressHUD hideProgressHUDInternal:self.view];
                NSString *subStr = [severDataDict.dataDict objectForKey:@"ResultString"];
                [MBProgressHUD showTextHudSub:subStr view:self.view];
            }
            
        }];
    }else{
        NSString *requestResultStr=@"没有更多数据了！";
        [MBProgressHUD showTextHudSub:requestResultStr view:self.view];
        [table.mj_footer endRefreshingWithNoMoreData];
        
    }
}




#pragma mark - TableView DateSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   
    return 1;
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return  45;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    SupplyAnalyzeDetailCell *cell = [SupplyAnalyzeDetailCell supplyAnalyzeDetailCellFromXib:tableView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
//    cell.nameLabel.text = model.companyName;
//    cell.countLabel.text =[NSString stringWithFormat:@"%@", model.accMCount];
//    cell.averagePriceLabel.text =[NSString stringWithFormat:@"%@", model.addMCount];
//    cell.moneyLabel.text =[NSString stringWithFormat:@"%@", model.conMCount];
    return cell;
}



#pragma mark - TableView Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}



@end
